//
//  MMPenStyleViewController.m
//  MemeMaker
//
//  Created by Phan Quang Ha on 7/12/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMPenStyleViewController.h"
#import "MMPaintingView.h"
#import "MMBasicPen.h"
#import <QuartzCore/QuartzCore.h>

@interface MMPenStyleViewController ()
@property (weak, nonatomic) IBOutlet UIButton *btnCancel;
@property (weak, nonatomic) IBOutlet UIButton *btnApply;
- (NSArray *) arrayColorComponents:(UIColor *) color;
@end

@implementation MMPenStyleViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSInteger minus = 0;
    if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        minus = -STATUS_BAR_HEIGHT;
    }
    
    CGRect frame1 = self.btnCancel.frame;
    frame1.origin.y += minus;
    self.btnCancel.frame = frame1;
    
    CGRect frame2 = self.btnApply.frame;
    frame2.origin.y += minus;
    self.btnApply.frame = frame2;
    
    // Do any additional setup after loading the view from its nib.
    _paint = [[MMPaintingView alloc] initWithFrame:CGRectMake(10, 64+minus, 300, 90)];
    _paint.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _paint.layer.borderWidth = 1.0;
    
    _paint.currentPen.pOpacity = 1.0f;
    _paint.currentPen.pSize = 10;
    NSArray *tmpArr = [self arrayColorComponents:[UIColor blackColor]];
    _paint.currentPen.pRed = [[tmpArr objectAtIndex:0] floatValue];
    _paint.currentPen.pGreen = [[tmpArr objectAtIndex:1] floatValue];
    _paint.currentPen.pBlue = [[tmpArr objectAtIndex:2] floatValue];
    
    [_paint drawPreview];
    _paint.userInteractionEnabled = NO;
    [self.view addSubview:_paint];
    
    MMStylePropertiesView *styleProperties = [MMUtils loadView:[MMStylePropertiesView class] FromNib:@"MMStylePropertiesView"];
    [styleProperties setDelegate:self];
    [styleProperties setFrame:CGRectMake(10.0,
                                         _paint.frame.origin.y + _paint.frame.size.height + 5.0,
                                         300.0,
                                         styleProperties.frame.size.height)];
    [self.view addSubview:styleProperties];
    
    
    MMColorBoard *colorBoard = [MMUtils loadView:[MMColorBoard class] FromNib:@"MMColorBoard"];
    [colorBoard setFrame:CGRectMake(10.0,
                                    styleProperties.frame.origin.y + styleProperties.frame.size.height + 5.0,
                                    300.0,
                                    112.0)];
    
    colorBoard.delegate = self;
    [self.view addSubview:colorBoard];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewDidUnload {
    [super viewDidUnload];
}


#pragma mark - IBAction
- (IBAction)apply:(id)sender {
    if (_delegate && [_delegate respondsToSelector:@selector(didFinishChangingPen:)]) {
        [_delegate didFinishChangingPen:_paint.currentPen];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (NSArray *) arrayColorComponents:(UIColor *) color {
    CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
    //for RGB
    if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
        [color getRed:&red green:&green blue:&blue alpha:&alpha];
    } else {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        red = components[0];
        green = components[1];
        blue = components[2];
        alpha = components[3];
    }
    // for non-RGB don't using this code for now
//    if(CGColorGetNumberOfComponents(color.CGColor) == 2) {
//        CGFloat hue;
//        CGFloat saturation;
//        CGFloat brightness;
//        [color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
//    }
    NSArray *tmpArr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:red], [NSNumber numberWithFloat:green], [NSNumber numberWithFloat:blue], [NSNumber numberWithFloat:alpha], nil];
    return tmpArr;
}


#pragma mark - ColorBoard Delegate
- (void) didSeletedColorBoard:(UIColor *)color {
    NSArray *tmpArr = [self arrayColorComponents:color];
    _paint.currentPen.pRed = [[tmpArr objectAtIndex:0] floatValue];
    _paint.currentPen.pGreen = [[tmpArr objectAtIndex:1] floatValue];
    _paint.currentPen.pBlue = [[tmpArr objectAtIndex:2] floatValue];
    [_paint drawPreview];
}

- (void) showBarColor {
    if (!colorBar) {
        NSInteger minus = 0;
        if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
            minus = -STATUS_BAR_HEIGHT;
        }
        colorBar = [[MMColorBar alloc] initWithFrame:CGRectMake(10.0,
                                                                388.0+minus,
                                                                300.0,
                                                                83.0)];
        [colorBar setDelegate:self];
        [self.view addSubview:colorBar];
    }
    [colorBar setHidden:NO];
    
}

- (void) hideBarColor {
    [colorBar setHidden:YES];
}

#pragma mark - Stype Properties Delegate
- (void) configWidth:(CGFloat)width {
    _paint.currentPen.pSize = width;
    
    [_paint drawPreview];
}

- (void) configOpacity:(CGFloat)opacity {
    _paint.currentPen.pOpacity = opacity;
    
    [_paint drawPreview];
}

#pragma mark - ColorBar Delegate
- (void) didSelectColor:(UIColor *)color {
    NSArray *tmpArr = [self arrayColorComponents:color];
    _paint.currentPen.pRed = [[tmpArr objectAtIndex:0] floatValue];
    _paint.currentPen.pGreen = [[tmpArr objectAtIndex:1] floatValue];
    _paint.currentPen.pBlue = [[tmpArr objectAtIndex:2] floatValue];
    [_paint drawPreview];
}

- (void) clearData {
    _paint.currentPen.pBlue = 0.0;
    _paint.currentPen.pGreen = 0.0;
    _paint.currentPen.pRed = 0.0;
}


@end
